package com.cmw.auntapp.presenter;

import android.app.Service;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.cmw.auntapp.constant.API;
import com.cmw.auntapp.constant.CommandType;
import com.cmw.auntapp.constant.Constant;
import com.cmw.auntapp.core.base.BasePresenter;
import com.cmw.auntapp.core.base.IBaseView;
import com.cmw.auntapp.core.util.SharedPreferencesUtil;
import com.cmw.auntapp.model.LocationModel;
import com.cmw.auntapp.service.LocationService;
import static com.cmw.auntapp.constant.Constant.AUNT_LOCATION_KEY;


/**
 * @desc: 定位业务类
 * @author: chengmingwei
 * @date: 2021/05/23 19:16
 * @contact: phone(微信同号):13922111489 / email: 340360491@qq.com
 */
public class LocationPresenter extends BasePresenter<LocationModel> {
    private static final String TAG = "LocationPresenter";

    private IBaseView iBaseView;

    public LocationPresenter(LocationService iBaseView) {
        this.iBaseView = (IBaseView)iBaseView;
        this.setContext((Service)iBaseView);
    }

    public LocationPresenter(IBaseView iBaseView) {
        this.iBaseView = iBaseView;
        init();
    }

    public void doSave(AMapLocation amapLocation) {
        SharedPreferencesUtil util = SharedPreferencesUtil.getInstance(getContext());
        Long memberId = util.getLong(Constant.USER_MEMBERID_KEY);
        Long auntId = util.getLong(Constant.USER_INEMPID_KEY);
        if (null == auntId || auntId.longValue() == 0L) {
            Log.e(TAG, "会员ID:"+memberId+"的阿姨还未实名认证，故无法更新其位置数据到服务端!");
            return;
        }
        LocationModel locationModel = new LocationModel();
        locationModel.setObjectId(auntId);
        locationModel.setLatitude(amapLocation.getLatitude());
        locationModel.setLongitude(amapLocation.getLongitude());
        locationModel.setProvince(amapLocation.getProvince());
        locationModel.setCity(amapLocation.getCity());
        locationModel.setArea(amapLocation.getDistrict());
        locationModel.setAnchor(amapLocation.getAoiName());
        locationModel.setAddress(amapLocation.getAddress());
        util.saveObject(AUNT_LOCATION_KEY, locationModel);
        doSave(locationModel);
    }

    /**
     * 直接从缓存中取出位置数据更新保存（适用于阿姨实名后更新）
     */
    public void doSave() {
        SharedPreferencesUtil util = SharedPreferencesUtil.getInstance(getContext());
        LocationModel locationModel = util.getObject(AUNT_LOCATION_KEY, LocationModel.class);
        Long memberId = util.getLong(Constant.USER_MEMBERID_KEY);
        Long auntId = util.getLong(Constant.USER_INEMPID_KEY);
        if (null == auntId) {
            Log.e(TAG, "会员ID:"+memberId+"的阿姨还未实名认证，故无法更新其位置数据到服务端!");
            return;
        }
        Long objectId =  locationModel.getObjectId();
        if (null == objectId) {
            locationModel.setObjectId(auntId);
        }
        doSave(locationModel);
    }

    /**
     * 保存阿姨定位信息
     * @param locationModel 定位信息
     */
    public void doSave(LocationModel locationModel) {
        postBody(API.LOCATION_SAVE, locationModel, CommandType.SAVE);
    }


    @Override
    protected IBaseView getView() {
        return iBaseView;
    }


}
